import {addShipping,queryCityIsExist,findParentNode,modFreightTemplate,updateShippingTemplet,addFrightTemplet,queryProvice,delShippingTempletAttr,modSellerShippingStatus,queryFreightTemplate, queryShipping, modShipping, delShipping, queryAllShipping,querySeller} from '../services/shipping';

export default {
    namespace: 'shipping',

    state: {
        data: {
            list: [],
            pagination: {},
        },
        typeSList: [],
        loading: true,
        dataShipping:[],
    },

    effects: {
        * fetch({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryShipping, payload);
            yield put({
                type: 'save',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * queryShipping({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryShipping, payload);
            yield put({
                type: 'saveShipping',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        *queryCityIsExist({payload, callback}, {call, put}) {

            const response = yield call(queryCityIsExist, payload);

            if (callback) callback(response);
        },
        *findParentNode({payload, callback}, {call, put}) {
            // yield put({
            //     type: 'changeLoading',
            //     payload: true,
            // });
            const response = yield call(findParentNode, payload);
            // yield put({
            //     type: 'changeLoading',
            //     payload: false,
            // });

            if (callback) callback(response);
        },
        *queryFreightTemplate({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryFreightTemplate, payload);
            yield put({
                type: 'saveFreightTemplate',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },

        * Sellerfetch({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(querySeller, payload);
            yield put({
                type: 'saveSeller',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * add({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(addFrightTemplet, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });
            if (callback) callback(response);
        },
        * mod({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(modFreightTemplate, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        * modSellerShippingStatus({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(modSellerShippingStatus, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        * updateShippingTemplet({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(updateShippingTemplet, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        * delShippingTempletAttr({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(delShippingTempletAttr, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },

        * del({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(delShipping, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        * queryAll({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryAllShipping, payload);
            yield put({
                type: 'save',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
            if (callback) callback(response);
        },
    },

    reducers: {
        save(state, action) {
            const typeSList = [];
            getTypeSList(typeSList, action.payload.resultData.shippingList.list);
            function getTypeSList(tmpR, list) {
                for (var i = 0; i < list.length; i++) {
                    const typeItem = {value: list[i]["shipping_id"], label: list[i]["shipping_name"]};
                    if (list[i]["children"]) {
                        typeItem["children"] = [];
                        getTypeSList(typeItem["children"], list[i]["children"]);
                    }
                    tmpR.push(typeItem);
                }
            }

            return {
                ...state,
                typeSList: typeSList,
                data: action.payload.resultData.shippingList,
            };
        },
        saveShipping(state, action) {
            const typeSList = [];
            getTypeSList(typeSList, action.payload.resultData.shippingList.list);
            function getTypeSList(tmpR, list) {
                for (var i = 0; i < list.length; i++) {
                    const typeItem = {value: list[i]["shipping_id"], label: list[i]["shipping_name"]};
                    if (list[i]["children"]) {
                        typeItem["children"] = [];
                        getTypeSList(typeItem["children"], list[i]["children"]);
                    }
                    tmpR.push(typeItem);
                }
            }

            return {
                ...state,
                typeList: typeSList,
                dataShipping: action.payload.resultData.shippingList.list,
            };
        },

        saveFreightTemplate(state, action) {
            const typeSList = [];
            getTypeSList(typeSList, action.payload.resultData.FreightTemplateList.list);
            function getTypeSList(tmpR, list) {
                for (var i = 0; i < list.length; i++) {
                    const typeItem = {value: list[i]["shipping_templet_attr_id"], label: list[i]["shipping_templet_name"]};
                    if (list[i]["children"]) {
                        typeItem["children"] = [];
                        getTypeSList(typeItem["children"], list[i]["children"]);
                    }
                    tmpR.push(typeItem);
                }
            }
            return {
                ...state,
                typeSList:getTypeSList,
                data: action.payload.resultData.FreightTemplateList,
            };
        },


        saveProvice(state, action) {

         return {
                ...state,
                data: action.payload.resultData.proviceList,
            };

},
        saveSeller(state, action) {
            const typeSList = [];
            getTypeSList(typeSList, action.payload.resultData.sellerList.list);
            function getTypeSList(tmpR, list) {
                for (var i = 0; i < list.length; i++) {
                    const typeItem = {value: list[i]["id"], label: list[i]["shop_name"]};
                    if (list[i]["children"]) {
                        typeItem["children"] = [];
                        getTypeSList(typeItem["children"], list[i]["children"]);
                    }
                    tmpR.push(typeItem);
                }
            }

            return {
                ...state,
                typeSList: typeSList,
                data: action.payload.resultData.sellerList,
            };
        },


        changeLoading(state, action) {
            return {
                ...state,
                loading: action.payload,
            };
        },
    },

};
